package com.skywolf.chem.achievement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.skywolf.chem.achievement.entity.CtSportAchievement;
import com.skywolf.chem.achievement.entity.CtSysAchievement;
import com.skywolf.chem.achievement.mapper.CtSysAchievementMapper;
import com.skywolf.chem.achievement.pojo.CtAchievementMatchPageParam;
import com.skywolf.chem.achievement.pojo.CtSysAchievementMatchDto;
import com.skywolf.chem.achievement.service.ICtSportAchievementService;
import com.skywolf.chem.achievement.service.ICtSysAchievementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.skywolf.chem.common.config.data.DataSource;
import com.skywolf.chem.common.config.data.DataSourceNames;
import com.skywolf.chem.common.sysconfig.pojo.vo.PageVO;
import com.skywolf.chem.match.common.page.PageInfo;
import com.skywolf.chem.match.params.vo.MatchPageInfoQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 运动成就系统配置表 服务实现类
 * </p>
 *
 * @author 
 * @since 2021-11-08
 */
@Service
public class CtSysAchievementServiceImpl extends ServiceImpl<CtSysAchievementMapper, CtSysAchievement> implements ICtSysAchievementService {
    @Autowired
    CtSysAchievementMapper ctSysAchievementMapper;
    @Autowired
    ICtSportAchievementService sportAchievementService;


    @Override
    @DataSource(DataSourceNames.TWO)
    public PageVO<CtSysAchievementMatchDto> getMatchPageList(CtAchievementMatchPageParam pageParam) {
        Page<CtSysAchievementMatchDto> page=new PageInfo<>(pageParam);
        IPage<CtSysAchievementMatchDto> iPage=ctSysAchievementMapper.getMatchPageList(page,pageParam);
        List<CtSysAchievementMatchDto> list = iPage.getRecords();
        for (CtSysAchievementMatchDto item:list) {
            //统计赛事奖牌的获得情况
            QueryWrapper<CtSportAchievement> queryWrapper=new QueryWrapper();
            queryWrapper.eq("achievement_id",item.getId());
            Integer count = sportAchievementService.count(queryWrapper);
            item.setNumber(count);
        }
        iPage.setRecords(list);
        return new PageVO<>(iPage);
    }

    @Override
    @DataSource(DataSourceNames.TWO)
    public void addAchievement(CtSysAchievement ctSysAchievement) {
        this.save(ctSysAchievement);
    }

    @Override
    @DataSource(DataSourceNames.TWO)
    public void updateAchievement(CtSysAchievement ctSysAchievement) {
        this.updateById(ctSysAchievement);
    }

    @Override
    @DataSource(DataSourceNames.TWO)
    public List<CtSysAchievement> getAll() {
        return ctSysAchievementMapper.getAll();
    }
}
